package jut.concurrent.singleton;

public class SingletonVolatile {
    //DCL双端检索机制 存在指令重排的可能性 通过补充volatile解决这个问题
    private static volatile SingletonVolatile instance = null;

    private SingletonVolatile() {
        System.out.println(Thread.currentThread().getName() + "构造方法");
    }

    public static SingletonVolatile getInstance() {
        if (instance == null) {
            synchronized (SingletonVolatile.class) {
                if (instance == null) {
                    instance = new SingletonVolatile();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int index = 1;index <= 10 ;index ++){
            new Thread(() -> {
                SingletonVolatile.getInstance();
            },String.valueOf(index)).start();
        }
    }
}
